from login import use_default_login
from collect import CollectPublisherThread
from generate import ReportGenerator
import utils


# 获取一段时间类的流量主
def collect_and_generate(start_date, end_date, thread=8):
    library = utils.default_library()
    service = use_default_login()
    account_chunks = library.get_account_chunks(thread)
    chunk_count = 1
    print('获取并生成{0}到{1}的流量主'.format(start_date, end_date))
    for chunk in account_chunks:
        print('=第{}批线程开始='.format(chunk_count))
        pool = []
        for gh_id in chunk:
            account = library.get_account_data(gh_id)
            thread = CollectPublisherThread(
                service, account, start_date, end_date)
            thread.start()
            pool.append(thread)
        for thread in pool:
            thread.join()
        print('=第{}/{}批线程结束='.format(chunk_count, len(account_chunks)))
        chunk_count += 1
    print('=生成报告=')
    reporter = ReportGenerator(library, end_date)
    reporter.make_all_publisher(start_date, end_date)
    input('=运行结束，按回车键退出=')


if __name__ == '__main__':
    yesterday = utils.get_yesterday()
    start_date = input('请输入开始收集日期,默认为昨天({}): '.format(yesterday))
    end_date = input('请输入开始收集日期,默认为昨天({}): '.format(yesterday))
    collect_and_generate(start_date if start_date else yesterday,
                         end_date if end_date else yesterday)
